package com.ldc.framework.adapter

import android.annotation.SuppressLint
import android.os.Build
import android.text.Html
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.text.HtmlCompat
import androidx.recyclerview.widget.RecyclerView
import cn.bingoogolapple.bgabanner.BGABanner
import com.ldc.commonlibs.listener.OnRowListener
import com.ldc.commonlibs.listener.OnRowListener2
import com.ldc.commonres.common.BaseRvViewHolder
import com.ldc.commonres.uts.GlideUts
import com.ldc.commonres.uts.TagUts
import com.ldc.framework.R
import com.ldc.framework.model.ArticleModel
import com.ldc.framework.model.BannerModel

class HomeListAdapter : RecyclerView.Adapter<BaseRvViewHolder>() {
    companion object {
        private const val BANNER_VIEW_CODE = 0x123
        private const val DATA_VIEW_CODE = 0x234
    }

    private var onRowListener: OnRowListener<ArticleModel>? = null
    private var onBannerListener: OnRowListener<BannerModel>? = null
    private var onCollectListener2: OnRowListener2<ArticleModel, Int>? = null

    fun addOnRowListener(listener: OnRowListener<ArticleModel>) {
        this.onRowListener = listener
    }

    fun addOnBannerListener(listener: OnRowListener<BannerModel>) {
        this.onBannerListener = listener
    }

    fun addCollectRowListener(listener: OnRowListener2<ArticleModel, Int>) {
        this.onCollectListener2 = listener
    }

    private var bannerList: MutableList<BannerModel> = mutableListOf()
    private var articleList: MutableList<ArticleModel> = mutableListOf()

    fun isEmptyData(): Boolean = (articleList.size + bannerList.size) == 0

    // 设置
    fun newBannerData(bannerList: ArrayList<BannerModel>?) {
        if (bannerList.isNullOrEmpty()) this.bannerList.clear()
        else
            this.bannerList = ArrayList(bannerList)
        notifyItemChanged(0, this.bannerList)
    }

    @SuppressLint("NotifyDataSetChanged")
    fun newArticleData(articleList: ArrayList<ArticleModel>?) {
        if (articleList.isNullOrEmpty()) this.articleList.clear()
        else
            this.articleList = ArrayList(articleList)
        notifyDataSetChanged()
    }

    @SuppressLint("NotifyDataSetChanged")
    fun addArticleData(articleList: ArrayList<ArticleModel>?) {
        if (articleList.isNullOrEmpty()) return
        this.articleList.addAll(articleList)
        notifyDataSetChanged()
    }


    // 修改收藏
    @SuppressLint("NotifyDataSetChanged")
    fun changeDataItem(position: Int) {
        val data = articleList[position]
        articleList[position].collect = !data.collect
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseRvViewHolder {
        return if (viewType == BANNER_VIEW_CODE) BaseRvViewHolder.Companion.createViewHolder(
            parent,
            R.layout.framework_layout_view_item_banner
        )
        else if (viewType == DATA_VIEW_CODE) BaseRvViewHolder.Companion.createViewHolder(
            parent,
            R.layout.framework_layout_view_item_article
        )
        else
            BaseRvViewHolder.Companion.createViewHolder(
                parent,
                com.ldc.commonres.R.layout.res_common_status_view
            )
    }

    override fun onBindViewHolder(holder: BaseRvViewHolder, position: Int) {
        when (getItemViewType(position)) {
            BANNER_VIEW_CODE -> {
                //banner
                val bgaBanner: BGABanner = holder.findView(R.id.banner) as BGABanner
                bgaBanner.also { banner ->
                    banner.setAdapter(object : BGABanner.Adapter<ImageView, String> {
                        override fun fillBannerItem(
                            banner: BGABanner?,
                            itemView: ImageView?,
                            model: String?,
                            position: Int
                        ) {
                            GlideUts.setImage(itemView!!.context, "$model", itemView, 0)
                        }
                    })
                    // 设置数据
                    banner.setData(
                        bannerList.map { it.imagePath }.toList(),
                        bannerList.map { it.title }.toList()
                    )
                    //
                    banner.setDelegate(object : BGABanner.Delegate<ImageView, String> {
                        override fun onBannerItemClick(
                            banner: BGABanner?,
                            itemView: ImageView?,
                            model: String?,
                            position: Int
                        ) {
                            val data: BannerModel = bannerList[position]

                            onBannerListener?.onRow(itemView!!, data)
                        }
                    })
                }
            }
            DATA_VIEW_CODE -> {
                val data: ArticleModel = articleList[position - 1]
                // 数据
                holder.setImage(R.id.iv_icon, data.envelopePic)
                holder.visibility(
                    R.id.iv_icon,
                    data.envelopePic.isNotBlank() || data.envelopePic != ""
                )
                holder.setText(R.id.tv_title, data.title)
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    val desc: CharSequence =
                        Html.fromHtml(data.desc, HtmlCompat.FROM_HTML_MODE_COMPACT)
                    holder.setText(
                        R.id.tv_desc,
                        desc
                    )
                } else {
                    holder.setText(R.id.tv_desc, data.desc)
                }
                holder.visibility(R.id.tv_desc, data.desc.isNotEmpty())
                holder.setText(R.id.tv_chapter_name, "${data.superChapterName}-${data.chapterName}")
                holder.setText(R.id.tv_author, data.author)
                holder.setText(R.id.tv_date, data.niceShareDate)
                holder.visibility(R.id.ll_fresh, data.fresh)
                holder.visibility(R.id.ll_zan, data.zan > 0)
                holder.setText(R.id.tv_zan, "${data.zan}")
                holder.setImage(
                    R.id.iv_collect,
                    if (data.collect) com.ldc.commonres.R.drawable.res_icon_collect else com.ldc.commonres.R.drawable.res_icon_un_collect
                )
                holder.setText(
                    R.id.tv_collect, if (data.collect) "已收藏" else "收藏"
                )
                holder.visibility(R.id.rv_tag_list, data.tags.isNotEmpty())
                if (data.tags.isNotEmpty()) {
                    TagUts.tagList(data.tags, holder.findView(R.id.rv_tag_list) as RecyclerView)
                }
                // 设置点击事件
                holder.addOnRowListener(holder.itemView, data, onRowListener)
                holder.addOnRowListener2(R.id.ll_collect, data, position - 1, onCollectListener2)
            }
        }

    }

    override fun getItemCount(): Int {
        return if (bannerList.isNullOrEmpty()) {
            if (articleList.isNullOrEmpty()) 0
            else articleList.size
        } else {
            if (articleList.isNullOrEmpty()) 1
            else articleList.size + 1
        }
    }


    override fun getItemViewType(position: Int): Int {
        // return super.getItemViewType(position)
        return if (position == 0) BANNER_VIEW_CODE else DATA_VIEW_CODE
    }
}